SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[VALIDA_LONG]  
/*--------------------------------------------------------------*/    
/*----- Empresa           : OFISIS S.A.                     ----*/    
/*----- Cliente           : OFISIS S.A.                     ----*/    
/*----- Sistema           : Contabilidad                    ----*/    
/*----- Modulo            : Administracion                  ----*/    
/*----- Programa          : Validacion del Plan de Cuentas  ----*/    
/*-----                     Valida Quiebres                 ----*/    
/*----- Nombre            : SP_TMPLAN_EMPR_Q04              ----*/    
/*----- Script            : TMPLAN_EMPR                     ----*/    
/*----- Desarrollado por  : Martin Garcia Farfan            ----*/    
/*----- Fecha             : 19/08/1999                      ----*/    
/*----- Base de Datos     : MS SQL Server                   ----*/    
/*----- Version BD        : 7.0                             ----*/    
/*--------------------------------------------------------------*/    
/*----- DROP PROCEDURE SP_TMPLAN_EMPR_Q04                   ----*/    
/*----- GRANT ALL ON SP_TMPLAN_EMPR_Q04 TO PUBLIC           ----*/    
/*----- EXEC VALIDA_LONG '01'                            ----*/    
/*--------------------------------------------------------------*/    
 @ISCO_EMPR       Varchar(02)    
As    
    
 Declare    
  @VSDE_QUIE        Varchar(20),    
  @VNNU_QUIE        Int,    
  @VNNU_INFO        Int,    
  @CSCO_CNTA_EMPR   Varchar(10),    
  @CSDE_CNTA_EMPR   Varchar(80),    
  @VSCO_CNTA        Varchar(10),    
  @VNNU_TOTA        Int,    
  @VNNU_CONT        Int    
    
 Create table #TWPLAN_EMPR_Q04    
  (CO_CNTA_EMPR   Varchar(10),    
   DE_CNTA_EMPR   Varchar(80),    
   NU_QUIE        int)    
    
select @VNNU_INFO =7,  @VSDE_QUIE = '0203040507'

SELECT @ISCO_EMPR, @VNNU_INFO

 Declare CU_TMPLAN_EMPR_Q04_1 Cursor    
  For   
    Select CO_CNTA_EMPR, DE_CNTA_EMPR    
    From data_gmi    
     Where CO_EMPR = @ISCO_EMPR    
      And len(CO_CNTA_EMPR) = @VNNU_INFO
     
 Select @VNNU_CONT = 0    
 Select @VNNU_QUIE = 0    
 Select @VNNU_TOTA = 0    
    
 Open CU_TMPLAN_EMPR_Q04_1    
 Fetch CU_TMPLAN_EMPR_Q04_1 INTO @CSCO_CNTA_EMPR, @CSDE_CNTA_EMPR    
      
 While @@Fetch_Status = 0     
  Begin     

   Select @VNNU_CONT = Datalength(RTRIM(@VSDE_QUIE));       
   Select @VNNU_CONT = @VNNU_CONT - 2;    
   Select @VNNU_QUIE = Convert(int, SUBSTRING(@VSDE_QUIE, @VNNU_CONT, 1));    
   While @VNNU_CONT > 0    
    Begin    
     Select @VSCO_CNTA = Substring(@CSCO_CNTA_EMPR, 1, @VNNU_QUIE);    
/*     Select @VNNU_TOTA = COUNT(*)    
      From TMPLAN_EMPR    
     Where CO_EMPR = @ISCO_EMPR    
     And CO_CNTA_EMPR = @VSCO_CNTA    
     And Datalength(CO_CNTA_EMPR) = @VNNU_QUIE    
     and NU_ANNO =YEAR(GETDATE());*/    
    
     Select @VNNU_TOTA = COUNT(*)    
      From data_gmi    
     Where CO_EMPR = @ISCO_EMPR    
     And CO_CNTA_EMPR = @VSCO_CNTA    
     And len(CO_CNTA_EMPR) = @VNNU_QUIE;    
    
    IF @VNNU_TOTA = 0    
      Insert into #TWPLAN_EMPR_Q04(CO_CNTA_EMPR,     
         DE_CNTA_EMPR, NU_QUIE)    
       Values(@VSCO_CNTA, @CSDE_CNTA_EMPR, @VNNU_QUIE)    
    
     Select @VNNU_CONT = @VNNU_CONT - 2;    
     Select @VNNU_QUIE = Convert(INT, Substring(@VSDE_QUIE, @VNNU_CONT, 1));    
   End    
         
   Fetch CU_TMPLAN_EMPR_Q04_1 INTO @CSCO_CNTA_EMPR, @CSDE_CNTA_EMPR    
 End    
    
 Close CU_TMPLAN_EMPR_Q04_1    
 Deallocate CU_TMPLAN_EMPR_Q04_1    
    
Select distinct CO_CNTA_EMPR, nu_quie
 from #TWPLAN_EMPR_Q04    
 Order By CO_CNTA_EMPR    
    
Return    
/*------------------------- Fin ------------------------------*/    
    
  
  
  
  
GO
